package org.example.struct.array;

import java.util.Arrays;

/**
 * @author liyishan
 * @date 2024/8/1 09:50
 * @apiNote
 */

public class LCP40MaxmiumScore {
    public int maxmiumScore(int[] cards, int cnt) {
        Arrays.sort(cards);
        int len = cards.length;
        if (len < cnt) {
            return 0;
        }
        int sum = 0;
        int minOdd = 1001;
        int minEven = 1001;
        for (int i = len - 1; i >= len - cnt; i--) {
            int cur = cards[i];
            if (cur % 2 == 1) {
                minOdd = Math.min(minOdd, cur);
            } else {
                minEven = Math.min(minEven, cur);
            }
            sum += cur;
        }
        if (sum % 2 == 0) {
            return sum;
        } else {
            int maxOdd = -1;
            int maxEven = -1;
            for (int i = len - cnt - 1; i >= 0; i--) {
                int cur = cards[i];
                if (cur % 2 == 1) {
                    maxOdd = Math.max(maxOdd, cur);
                } else {
                    maxEven = Math.max(maxEven, cur);
                }
            }
            //剩下的数里有最大偶数 如果没有就是0   如果有则看下前cnt个数中有没有最小奇数 如果没有就是0 如果有就是sum-minOdd+maxEven
            int ans1 = maxEven == -1 ? 0 : minOdd == 1001 ? 0 : sum - minOdd + maxEven;
            int ans2 = maxOdd == -1 ? 0 : minEven == 1001 ? 0 : sum - minEven + maxOdd;
            return Math.max(ans1, ans2);
        }
    }
}
